## This script: Partisanship by national news readership using GLES 2009
## ORIGINAL DATA IS AVAILABLE FROM GESIS: https://search.gesis.org/research_data/ZA5300 
## GLES (2019). Pre-election Cross Section (GLES 2009). GESIS Data Archive, Cologne. ZA5300 Data file Version 5.0.2, https://doi.org/10.4232/1.13228.

library(tidyverse)
library(haven)

rm(list = ls())

## Load Data
gles <- haven::read_dta('data/GLES_2009_ZA5300_de_v5-0-2.dta')


## Variables that relate to national newspaper readership 
tz_vars <- c('q101a', 'q101b' , 'q101c',
             'q101d',  'q101e', 'q101f')
party_id <- c('q139a')

## Clean up 
gles <- gles %>% 
  dplyr::select(one_of(c(tz_vars, party_id))) %>%
  mutate_at(tz_vars, ~ ifelse(. == 1, 1, 0)) %>% 
  rename('read_bild' = 1,
         'read_faz' = 2,
         'read_welt' = 3,
         'read_sz' = 4,
         'read_fr' = 5,
         'read_taz' = 6,
         'party_id' = 7) %>%
  mutate(party_id = recode(as.character(party_id),
                           `808` = 'None',
                           `801` = 'Other',
                           `997` = 'Other',
                           `998` = 'None',
                           `2` = 'CDU/CSU',
                           `1` = 'CDU/CSU',
                           `3` = 'CDU/CSU',
                           `4` = 'SPD',
                           `5` = 'FDP',
                           `6` = 'Greens',
                           `7` = 'Linke'))
 
gles$party_id[gles$party_id == '999'] <- NA

## 
table(gles$party_id)

## 
readership_long <- gles %>%
  pivot_longer(contains('read'),
               names_to = 'paper',
               values_to = 'read') %>% 
  filter(read == 1) %>%
  filter(!is.na(party_id)) 


## Summarize by newspaper 
nobs_paper <- readership_long %>%
  group_by(paper) %>% 
  summarise(nobs_total = n())

## 

plot_df <- readership_long %>%
  group_by(paper) %>%
  count(party_id) %>% 
  left_join(nobs_paper) %>%
  mutate(share_party = n / nobs_total) %>%
  filter(!paper %in% c('read_taz', 'read_fr')) %>% 
  mutate(paper = tools::toTitleCase(str_replace_all(paper, 'read_', '')),
         paper = recode(paper,
                        `Fr` = 'Frankfurter Rundschau',
                        `Sz` = 'Süddeutsche Zeitung (SZ)',
                        `Taz` = 'Die Tageszeitung',
                        `Welt` = 'Die Welt',
                        `Faz` = 'Frankfurter Allgemeine Zeitung (FAZ)',
                        `Bild` = 'Bild Zeitung')) %>%
  ungroup() %>% 
  filter(party_id != 'Other') %>%
  mutate(party_id = fct_relevel(party_id, 
                                c('Linke','Greens', 'SPD', 'None', 'CDU/CSU','FDP')))

p1 <- ggplot(plot_df, aes(x = party_id, y = share_party)) + 
  facet_wrap(~ paper,
             nrow = 2) + 
  geom_bar(stat = 'identity',
           width = .8, color = 'black', fill = 'grey93') + 
  theme_bw() + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) + 
  labs(x = 'Party ID',
       y = 'Party ID share among readers')
p1
